package cn.imu.appdevexperiment2020.work04;

import android.os.Bundle;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;

import cn.imu.appdevexperiment2020.R;
import cn.imu.appdevexperiment2020.base.BaseActivity;
import cn.imu.appdevexperiment2020.utils.ActivityUtil;
import cn.imu.appdevexperiment2020.utils.HttpUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class UserInfoActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);

        HttpUtil.get(HttpUtil.ADDR + "/app/user/api/getById?userId=77", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                //response.body().string().
                JSONObject jsonObject = JSON.parseObject(response.body().string());
                if (jsonObject.get("code").toString().equals("200")) {

                    //这儿是耗时操作，完成之后更新UI；
                    runOnUiThread(() -> {
                        //更新UI
                        UserBean data = jsonObject.getObject("data", UserBean.class);
                        TextView youhuid = findViewById(R.id.yonghuid);
                        youhuid.setText(data.getUserid());
                        TextView yonghuming = findViewById(R.id.yonghuming);
                        yonghuming.setText(data.getUsername());
                        TextView nicheng = findViewById(R.id.nicheng);
                        nicheng.setText(data.getNickname());

                        TextView youxiang = findViewById(R.id.youxiang);
                        youxiang.setText(data.getEmail());

                        TextView dianhua = findViewById(R.id.dianhua);

                        dianhua.setText(data.getPhonenumber());
                    });


                    //System.out.println(data.toString());
                    //说明解析成功跳转界面
                } else {
                    System.out.println(jsonObject.get("code"));
                }

            }
        });

        findViewById(R.id.huoquliebiao).setOnClickListener((view) -> {
            ActivityUtil.getInstance().goActivity(this, UserListActivity.class);
        });

    }

}